package com.zzyl.nursing.service.impl;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.TimeVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 预约信息Service业务层处理
 * 
 * @author rouiqasy
 * @date 2025-01-15
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return reservationMapper.insert(reservation);
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return reservationMapper.deleteById(id);
    }

    /**
     * 获取用户预约总数
     *
     * @param userId 用户id
     * @return 取消预约总数
     */
    @Override
    public int cancelledCount(Long userId) {
        LocalDateTime startTime = LocalDate.now().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        long count = count(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getId, userId)
                .eq(Reservation::getStatus, 2)
                .between(Reservation::getCreateTime, startTime, endTime));
        return (int) count;
    }

    /**
     * 获取剩余预约次数
     *
     * @param time 时间
     * @return 每时间段剩余预约次数
     */
    @Override
    public List<TimeVo> getCountByTime(Long time) {
        //将long类型的时间戳转换为Instant对象
        Instant instant = Instant.ofEpochMilli(time);
        //获取预约时间
        LocalDateTime resTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        LocalDate localDate = resTime.toLocalDate();
        LocalDateTime start = localDate.atStartOfDay();
        LocalDateTime end = start.plusDays(1);
        List<TimeVo> timeVoList = reservationMapper.getCountByTime(resTime,start,end);
        return timeVoList;
    }

    /**
     * 添加预约信息
     *
     * @param reservation 预约信息
     */
    @Override
    public void addReservation(Reservation reservation) {
        reservation.setStatus(0);
        insertReservation(reservation);
    }

    /**
     * 分页查询预约信息
     *
     * @param pageNum  页码
     * @param pageSize 页面大小
     * @param status   状态
     * @return 预约信息分页信息
     */
    @Override
    public TableDataInfo<Reservation> getReservationByPage(Integer pageNum, Integer pageSize, Integer status) {
        Page<Reservation> page = new Page(pageNum, pageSize);
        page = page(page, Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, status))
                .addOrder(OrderItem.desc("create_time"));

        TableDataInfo<Reservation> dataInfo = createTableDataInfo(page);
        return dataInfo;
    }

    private TableDataInfo<Reservation> createTableDataInfo(Page page) {
        TableDataInfo<Reservation> dataInfo = new TableDataInfo<>();
        dataInfo.setCode(HttpStatus.SUCCESS);
        dataInfo.setMsg("查询成功");
        dataInfo.setRows(page.getRecords());
        dataInfo.setTotal(page.getTotal());
        return dataInfo;
    }

    /**
     * 取消预约
     *
     * @param id 预约id
     */
    @Override
    public void cancelReservation(Long id) {
        Reservation reservation = getOne(Wrappers.<Reservation>lambdaQuery().eq(Reservation::getId, id));
        reservation.setStatus(2);
        updateReservation(reservation);
    }
}
